package proxy.wrappers;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import com.genymobile.scrcpy.Ln;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/* loaded from: classes2.dex */
public class ActivityManager {
    private Method getContentProviderExternalMethod;
    private boolean getContentProviderExternalMethodNewVersion = true;
    private final IInterface manager;
    private Method removeContentProviderExternalMethod;

    public ActivityManager(IInterface manager) {
        this.manager = manager;
    }

    private Method getGetContentProviderExternalMethod() throws NoSuchMethodException {
        if (this.getContentProviderExternalMethod == null) {
            try {
                this.getContentProviderExternalMethod = this.manager.getClass().getMethod("getContentProviderExternal", String.class, Integer.TYPE, IBinder.class, String.class);
            } catch (NoSuchMethodException e) {
                this.getContentProviderExternalMethod = this.manager.getClass().getMethod("getContentProviderExternal", String.class, Integer.TYPE, IBinder.class);
                this.getContentProviderExternalMethodNewVersion = false;
            }
        }
        return this.getContentProviderExternalMethod;
    }

    private Method getRemoveContentProviderExternalMethod() throws NoSuchMethodException {
        if (this.removeContentProviderExternalMethod == null) {
            this.removeContentProviderExternalMethod = this.manager.getClass().getMethod("removeContentProviderExternal", String.class, IBinder.class);
        }
        return this.removeContentProviderExternalMethod;
    }

    private ContentProvider getContentProviderExternal(String name, IBinder token) {
        Object[] args;
        try {
            Method method = getGetContentProviderExternalMethod();
            if (this.getContentProviderExternalMethodNewVersion) {
                args = new Object[]{name, 0, token, null};
            } else {
                args = new Object[]{name, 0, token};
            }
            Object providerHolder = method.invoke(this.manager, args);
            if (providerHolder == null) {
                return null;
            }
            Field providerField = providerHolder.getClass().getDeclaredField("provider");
            providerField.setAccessible(true);
            Object provider = providerField.get(providerHolder);
            if (provider == null) {
                return null;
            }
            return new ContentProvider(this, provider, name, token);
        } catch (IllegalAccessException | NoSuchFieldException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
            return null;
        }
    }

    void removeContentProviderExternal(String name, IBinder token) {
        try {
            Method method = getRemoveContentProviderExternalMethod();
            method.invoke(this.manager, name, token);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
            Ln.e("Could not invoke method", e);
        }
    }

    public ContentProvider createSettingsProvider() {
        return getContentProviderExternal("settings", new Binder());
    }
}